package com.xfcy.system.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author 晓风残月Lx
 * @date 2023/6/11 16:00
 * 跨域（Spring自带的）
 */
@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsFilter corsFilter() {

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");   // 允许白名单域名进行跨域调用
        corsConfiguration.addAllowedMethod("*");   // 允许所有请求头
        corsConfiguration.addAllowedHeader("*");   // 允许全部原始头信息
        corsConfiguration.setAllowCredentials(true);  // 允许跨域发送cookie

        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsFilter(urlBasedCorsConfigurationSource);
    }


}
